package com.example.interviewassistant;

import android.content.Context;
import android.content.SharedPreferences;

public class ApiConfigManager {
    private static final String PREF_NAME = "api_config";
    private static final String KEY_API_URL = "api_url";
    private static final String KEY_API_KEY = "api_key";
    private static final String KEY_MODEL_TYPE = "model_type";
    
    // 默认模型，可选项
    public static final String MODEL_GPT35 = "gpt-3.5-turbo";
    public static final String MODEL_GPT4 = "gpt-4";
    public static final String MODEL_DEEPSEEK = "deepseek-chat";
    public static final String MODEL_DEFAULT = MODEL_DEEPSEEK;
    
    // 默认API地址
    public static final String API_URL_OPENAI = "https://api.openai.com/v1/chat/completions";
    public static final String API_URL_DEEPSEEK = "https://api.deepseek.com/v1/chat/completions";
    
    private Context context;
    
    public ApiConfigManager(Context context) {
        this.context = context;
    }

    /**
     * 获取API配置
     * @return ApiConfig对象，如果配置无效则返回null
     */
    public ApiConfig getApiConfig() {
        return loadConfig(context);
    }

    public static void saveConfig(Context context, ApiConfig config) {
        SharedPreferences sharedPreferences = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString(KEY_API_URL, config.getApiUrl());
        editor.putString(KEY_API_KEY, config.getApiKey());
        editor.putString(KEY_MODEL_TYPE, config.getModelType());
        editor.apply();
    }

    public static ApiConfig loadConfig(Context context) {
        SharedPreferences sharedPreferences = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
        String apiUrl = sharedPreferences.getString(KEY_API_URL, "");
        String apiKey = sharedPreferences.getString(KEY_API_KEY, "");
        String modelType = sharedPreferences.getString(KEY_MODEL_TYPE, MODEL_DEFAULT);
        
        // 如果URL为空，根据模型类型提供默认URL
        if (apiUrl.isEmpty() && !apiKey.isEmpty()) {
            if (modelType.equals(MODEL_DEEPSEEK)) {
                apiUrl = API_URL_DEEPSEEK;
            } else if (modelType.startsWith("gpt-")) {
                apiUrl = API_URL_OPENAI;
            }
        }
        
        if (apiUrl.isEmpty() && apiKey.isEmpty()) {
            return null;
        }
        
        return new ApiConfig(apiUrl, apiKey, modelType);
    }

    public static boolean hasValidConfig(Context context) {
        ApiConfig config = loadConfig(context);
        return config != null && !config.getApiUrl().isEmpty() && !config.getApiKey().isEmpty();
    }
    
    /**
     * 根据选择的模型类型获取推荐的API URL
     */
    public static String getDefaultApiUrlForModel(String modelType) {
        if (modelType.equals(MODEL_DEEPSEEK)) {
            return API_URL_DEEPSEEK;
        } else if (modelType.startsWith("gpt-")) {
            return API_URL_OPENAI;
        }
        return "";
    }
} 